﻿using System.Linq;
using Best.SmartCms.Core.Utility;
using Best.SmartCms.Dto.Response;
using Best.SmartCms.Portal.Models;
using Microsoft.AspNetCore.Mvc;

namespace Best.SmartCms.Portal.Components
{
    [ViewComponent(Name = "Pagination")]
    public class PaginationViewComponent : ViewComponent
    {
        public PaginationViewComponent()
        {
            
        }

        public IViewComponentResult Invoke(IPagedResponse pagedResponse)
        {
            var model = new PaginationModel
            {
                PageSize = pagedResponse.PageSize,
                PageIndex = pagedResponse.PageSize,
                TotalCount = pagedResponse.TotalCount,
                TotalPages = pagedResponse.TotalPages,
                Url = Request.Path.Value
            };

            var query = Request.Query;
            model.CurrentPageIndex = Request.Query["pageindex"].ToString().ToInt(1);
            var queryarr = query.Where(c => c.Key != "pageindex").ToList();
            string queryurl = string.Empty;
            foreach (var item in queryarr)
            {
                queryurl += "&" + item.Key + "=" + item.Value;
            }

            model.Query = queryurl;
            

            return View(model);
        }
    }
}
